
package com.insigma.business.qrcode.soldier.util;

import cn.hutool.core.date.DateUtil;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

import com.insigma.business.qrcode.common.util.LogUtil;
import com.insigma.business.qrcode.common.vo.DescEnum;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.beanutils.PropertyUtils;

import java.util.Date;
import java.util.List;
import java.util.Set;


@Slf4j
public class ResponseFilterUtil {
    public static final String           FULL_PATTERN             = "yyyy-MM-dd HH:mm:ss";
    
    public static JSONObject filterObj(Object obj, String[] props) {
        JSONObject jsonObject = new JSONObject();
        for (String prop : props) {
            String[] namePair = prop.split(":");
            Object o = null;
            try {
                o = PropertyUtils.getProperty(obj, namePair[0]);
            } catch (Exception e) {
                if (obj != null) {
                    LogUtil.warn(log, "类:" + obj.getClass() + ",  属性:" + namePair[0]);
                }
                o = null;
            }
            if (o != null) {
                /*  if (o instanceof DataDict) {
                    jsonObject.put(namePair.length > 1 ? namePair[1] : namePair[0], buildDataDict((DataDict) o));
                } */

                if (o instanceof DescEnum) {
                    jsonObject.put(namePair.length > 1 ? namePair[1] : namePair[0], buildEnum((DescEnum) o));
                } else if (o instanceof Date) {
                    jsonObject.put(namePair.length > 1 ? namePair[1] : namePair[0], buildDate((Date) o));

                } else if (o instanceof List || o instanceof Set) {
                    jsonObject.put(namePair.length > 1 ? namePair[1] : namePair[0], null);
                } /*else if( o instanceof String ){
                    if( namePair.length > 1 && 0 == namePair[1].indexOf("dd.")){
                        jsonObject.put(namePair[1],buildDD((String)o) );
                    }else{
                        jsonObject.put(namePair.length > 1 ? namePair[1] : namePair[0], o);
                    }
                }*/ else {
                    jsonObject.put(namePair.length > 1 ? namePair[1] : namePair[0], o.toString());
                }
            } else {
                jsonObject.put(namePair.length > 1 ? namePair[1] : namePair[0], o);
            }

        }
        return jsonObject;
    }

    
    public static JSONArray filterList(List objList, String[] props) {
        if (null == objList || objList.size() == 0) {
            return null;
        }
        final JSONArray jsonArray = new JSONArray();
        for (Object voObj : objList) {
            jsonArray.add(filterObj(voObj, props));
        }

        return jsonArray;
    }

    public static JSONObject buildEnum(DescEnum enumObject) {
        JSONObject enumJson = new JSONObject();
        enumJson.put("key", enumObject);
        enumJson.put("value", enumObject.getDesc());
        return enumJson;
    }
    /*public static JSONObject buildEnum(TitleEnum enumObject) {
        JSONObject enumJson = new JSONObject();
        enumJson.put("key", enumObject);
        enumJson.put("value", enumObject.getTitle());
        return enumJson;
    }

    public static JSONObject buildDD(String code) {
        JSONObject dd = new JSONObject();
        dd.put("code", code);
        dd.put("value", DataCache.getInstance().getValueByCode(code));
        return dd;
    }

    public static JSONObject buildDataDict(DataDict ddObject) {
        JSONObject ddJson = new JSONObject();
        ddJson.put("code", ddObject.getCode());
        ddJson.put("value", ddObject.getValue());
        return ddJson;
    }*/

    private static String buildDate(Date dateObject) {
        String formatDate = "";
        if (null != dateObject) {
            formatDate = DateUtil.format((dateObject),FULL_PATTERN);
        }
        return formatDate;
    }

   /* public static JSONObject enumToJson(JSONObject body, TitleEnum enumObject, String bodyKey) {
        JSONObject enumJson = new JSONObject();
        enumJson.put("key", enumObject);
        enumJson.put("value", enumObject.getTitle());
        body.put(bodyKey, enumJson);
        return body;
    }*/

}
